#coding=utf-8 
from django.shortcuts import render
from django import forms
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
from django.template import RequestContext
from account.models import User
from django.forms.widgets import Widget

# Create your views here.

class UserForm(forms.Form):
    username = forms.CharField(label = '用户名: ', max_length=50)
    password = forms.CharField(label = '密码: ', widget = forms.PasswordInput())
    email = forms.EmailField(label = '电子邮件: ')
    mobile = forms.IntegerField(label = '手机: ')

class UserLoginForm(forms.Form):
    username = forms.CharField(label = '用户名: ', max_length=50)
    password = forms.CharField(label = '密码: ', widget = forms.PasswordInput())
    
def register(request):
    if request.method == "POST":
        uf = UserForm(request.POST)
        if uf.is_valid():
            #get 
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            email = uf.cleaned_data['email']
            mobile = uf.cleaned_data['mobile']
            #if exists
            if User.objects.get(username = username) != None:
                return HttpResponse("The Username has been already registered.")
            #write 
            user = User()
            user.username = username
            user.password = password
            user.email = email
            user.mobile = mobile
            user.save()
            #return to the result Page
            #return render_to_response('success.html', {'username':username}, context_instance=RequestContext(request))
            return render(request, 'account_success.html', {'username':username})
    else:
        uf = UserForm()
    #return render_to_response('register.html', {'uf':uf}, context_instance=RequestContext(request))
    return render(request, 'account_register.html', {'uf' : uf})

def login(request):
    if request.method == 'POST':
        uf = UserLoginForm(request.POST)
        if uf.is_valid():
            input_username = uf.cleaned_data['username']
            input_password = uf.cleaned_data['password']
            #check the user password
            user = User.objects.get(username = input_username)
            #user = User.objects.filter(username__exact = username, password__exact = password)
            if user.password == input_password:
                response = HttpResponseRedirect('/account/index/')
                request.session.set_expiry(300)
                request.session['username'] = user.username
                return response 
            else:
                return HttpResponse("Your username and password didn't match.")
    else:
        uf = UserLoginForm()
    return render(request, 'account_login_bootstrap.html', {'uf':uf})
    
def index(request):
    username = request.session.get('username', 'anybody')
    return render(request, 'account_index.html', {'username':username})

def logout(request):
    try:
        del request.session['username']
    except KeyError:
        pass
    #return HttpResponse('logout!!')
    return render(request, 'account_logout.html')